class PosHandler {
  constructor(imgSprite) {
    this._imgSprite = imgSprite;
    this._scaleBefore = null;
    this._mousePos = {};
    this._limitSize = 0;
    this.init();
  }

  init() {
    const imgSprite = this._imgSprite;
    this.initPos = this.getInitPosFactory();
    const { left, top, scale } = this.initPos.pos;
    imgSprite.left = left;
    imgSprite.top = top;
    imgSprite.scale = imgSprite.minScale = scale;
  }

  /**
   * 设置缩放比例
   * @param scale
   * @param mousePos 非必须  鼠标在canvas里的位置
   */
  doScale(scale = 0.1, mousePos) {
    const sprite = this._imgSprite;
    this.scaleBefore = sprite.scale;
    sprite.scale += scale;
    if (sprite.scale <= sprite.minScale) {
      sprite.scale = sprite.minScale;
    }
    const maxScale = 4;
    if (sprite.scale >= maxScale ) {
      sprite.scale = maxScale;
    }
    this.mousePos = mousePos;
    sprite.left = this.left;
    sprite.top = this.top;
    this.limitPos();
  }

  /**
   * 设置旋转度数
   * @param degree
   */
  rotateByDegree(degree) {
    const sprite = this._imgSprite;
    if (degree > 0) {
      if (sprite.rotate === 360) {
        sprite.rotate = 0;
      }
    } else {
      if (sprite.rotate === 0) {
        sprite.rotate = 360;
      }
    }
    sprite.rotate += degree;
    sprite.scale = 1;
    this.init();
  }
  /**
   * 位移
   * @param left
   * @param top
   */
  move({ left = 0, top = 0}) {
    if (this._imgSprite.xOverflow) {
      this._imgSprite.left += left;
    }
    if (this._imgSprite.yOverflow) {
      this._imgSprite.top += top;
    }
  }

  get left() {
    const sprite = this._imgSprite;
    if (!sprite.xOverflow) { // 如果渲染的图片没有超过画布大小
      this.initPos.scale = sprite.scale;
      return this.initPos.left;
    } else {  // 如果当前渲染的图片超过画布大小
      const { left = sprite.canvasWidth / 2 } = this._mousePos;
      /* 获取鼠标x方向在图片中的百分比 */
      const percent = (left - sprite.left) / (sprite.actualWidth * this._scaleBefore);
      return left - sprite.actualWidth * sprite.scale * percent;
    }
  }

  get top() {
    const sprite = this._imgSprite;
    if (!sprite.yOverflow) {
      this.initPos.scale = sprite.scale;
      return this.initPos.top;
    } else {
      const { top = sprite.canvasHeight / 2 } = this._mousePos;
      const percent = (top - sprite.top) / (sprite.actualHeight * this._scaleBefore); // 获取鼠标y方向在图片中的百分比
      return top - sprite.actualHeight * sprite.scale * percent;
    }
  }

  limitPos() {
    const sprite = this._imgSprite;
    if (sprite.xOverflow) {
      const actualWidth = sprite.actualWidth * sprite.scale;
      if (sprite.left > this._limitSize) {
        sprite.left = this._limitSize;
      } else if ( actualWidth + sprite.left < sprite.canvasWidth - this._limitSize) {
        sprite.left = sprite.canvasWidth - actualWidth - this._limitSize;
      }
    }
    if (sprite.yOverflow) {
      const actualHeight = sprite.actualHeight * sprite.scale;
      if (sprite.top > this._limitSize) {
        sprite.top = this._limitSize;
      } else if ( actualHeight + sprite.top < sprite.canvasHeight - this._limitSize) {
        sprite.top = sprite.canvasHeight - actualHeight - this._limitSize;
      }
    }
  }

  /**
   * 获取初始化的位置
   * @returns {{top: number, left: number}|{top: number, left: number, scale: *}|{top: number, left: number, scale: *}|{top: number, left: number, scale: *}}
   */
  getInitPosFactory() {
    const sprite = this._imgSprite;
    const actualWidth = sprite.actualWidth * sprite.scale;
    const actualHeight = sprite.actualHeight * sprite.scale;
    const canvasWidth = sprite.canvasWidth;
    const canvasHeight = sprite.canvasHeight;
    if (actualWidth <= canvasWidth && actualHeight <= canvasHeight) {
      return new SmallPos(sprite);
    } else if (actualWidth > canvasWidth && actualHeight > canvasHeight) {
      return new BothLargerPos(sprite);
    } else if (actualWidth > canvasWidth) {
      return new WidthLargerPos(sprite);
    } else if (actualHeight > canvasHeight) {
      return new HeightLargerPos(sprite);
    }
  }

  set scaleBefore(scaleBefore) {
    this._scaleBefore = scaleBefore;
  }

  set mousePos(mousePos) {
    this._mousePos = mousePos;
  }
}

